<?php

class ProductType extends AppModel {

    public $name = 'ProductType';
    public $useTable = 'product_type';
    public $primaryKey = 'id';
    public $validate = array(
        'name' => array(
            'rule' => 'notEmpty'
        )
    );

    /**
     *
     * @param type $id 
     */
    public function getDetailProductType($id) {
        $record = $this->findByid($id);
        if ($record) {
            App::import('model', 'ProductTypeProperty');
            $ob = new ProductTypeProperty();
            $result = $ob->find("all", array('conditions' => array('product_type' => $id)));
            $properties = array(); //echo "<pre>";var_dump($result);die();
            if ($result) {
                foreach ($result as $value) {
                    $properties[$value['ProductTypeProperty']['property']]['property'] = 1;
                }
            }
            $record['ProductTypeProperty'] = $properties;
        }
        return $record;
    }

    /**
     * 
     */
    public function afterSave($created, $options = array()) {
        parent::afterSave($created, $options);
        App::import('model', 'ProductTypeProperty');
        $ob = new ProductTypeProperty();
        if (!$created) {
            $ob->deleteAll(array('product_type' => $this->id));
        }
        if (isset($this->data['ProductTypeProperty'])) {
            //echo "<pre>";var_dump($this->data['ProductTypeProperty']);die();
            foreach ($this->data['ProductTypeProperty'] as $key => $value) {
                if ($value['property']) {
                    $ob->create();
                    $data = array(
                        'property' => $key,
                        'product_type' => $this->id
                    );
                    $ob->save(array('ProductTypeProperty' => $data));
                }
            }
        }
    }

    /**
     * 
     */
    public function getPropertyOfProductType($productType) {
        $return = array();
        $sql = "select PropertyLanguage.name property_name,Property.property_id property_id ,Property.type type";
        $sql .= " from product_type_property as ProductTypeProperty,property as Property,property_language as PropertyLanguage";
        $sql .= " WHERE ProductTypeProperty.property = Property.property_id AND Property.property_id = PropertyLanguage.property_id";
        $sql .= " AND  PropertyLanguage.language_id = 'eng' AND ProductTypeProperty.product_type = ?";
        $results = $this->getDataSource()->fetchAll($sql,array($productType));
        foreach ($results as $value) {

            $return[] = array(
                'property_id' => $value['Property']['property_id'],
                'name' => $value['PropertyLanguage']['property_name'],
                'type' => $value['Property']['type'],
                'values' => $this->getValuesOfProperty($value['Property']['property_id']),
            );
        }
        return $return;
    }

    /**
     * 
     */
    public function getValuesOfProperty($property_id) {
        App::import('model', 'PropertyValue');
        $ob = new PropertyValue();
        //get property value
        $arr_optin_select = array();
        $arr_option = $ob->getValues($property_id);
        if (isset($arr_option)) {
            foreach ($arr_option as $val) {
                $arr_optin_select[$val['pv']['property_value_id']] = $val['pvl']['value'];
            }
        }
        return $arr_optin_select;
    }

}
